﻿using AutoMapper;
using B.S.AuthCenter.Api.Write.Application.Command.Users;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;
using System.Transactions;

namespace B.S.AuthCenter.Api.Write.Application.Handler.Users
{
    public class DelUsersHandler : IRequestHandler<DelUsersCommand, ApiResult<int>>
    {
        IBaseRepository<UserModel> userRepo;
       
        IMapper mapper;

        public DelUsersHandler(IBaseRepository<UserModel> userRepo, IMapper mapper)
        {
            this.userRepo = userRepo;
           
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(DelUsersCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
           
            var users = userRepo.GetAll().Where(c => c.Id == request.Id).FirstOrDefault();
            if (users == null)
            {
               
                res.Msg = "用户不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            users.IsDeleted = true;
            users = mapper.Map(request, users);
            int a = userRepo.Update(users).Result;
            if (a > 0)
            {
                
                res.Code = ApiEnum.Success;
                res.Msg = "删除用户信息成功";
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                
                res.Code = ApiEnum.Fail;
                res.Msg = "删除用户信息失败";
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
